/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ParmView.java
 *
 * Created on May 14, 2009, 4:52:22 PM
 */

package viewer.parameters;

import java.util.*;
import java.awt.Dimension;
import java.text.DecimalFormat;

import simframe.simulation.SimParameter;

/**
 *
 * @author Steve Noble
 */
public class ParmView extends javax.swing.JFrame {

      private final static DecimalFormat numFormat = new DecimalFormat("#0.0");
    
    /** Creates new form ParmView */
    public ParmView(List<SimParameter> parmList) {
   
        initComponents();

        this.setResizable(false);
        
        Map<String, List<SimParameter>> parmGroups = new HashMap<String, List<SimParameter>>();

        // first create tabs
        for(SimParameter parm : parmList) {

            if (!parmGroups.containsKey(parm.getGroup())) {
                parmGroups.put(parm.getGroup(), new ArrayList<SimParameter>());
            }

             List<SimParameter> parms = parmGroups.get(parm.getGroup());

             parms.add(parm);
        }

        parmPane = new javax.swing.JTabbedPane();

        // create parameters
        createLayout(parmGroups);

        this.add(parmPane);

       this.pack();
    }

    private void createLayout(Map<String, List<SimParameter>> parmGroups) {

        java.awt.Font font = java.awt.Font.decode("tahoma-bold-14");
        Dimension horizFill = new Dimension(50,0);
        Dimension vertFill = new Dimension(0,5);
        Dimension preFill = new Dimension(10,0);

        for (String group : parmGroups.keySet()) {

            javax.swing.JPanel tabPanel = new javax.swing.JPanel();

            javax.swing.BoxLayout tabLayout = new javax.swing.BoxLayout(tabPanel, javax.swing.BoxLayout.PAGE_AXIS);

            tabPanel.setLayout(tabLayout);

            for (final SimParameter parm : parmGroups.get(group)) {

                javax.swing.JPanel parmPanel = new javax.swing.JPanel();

                parmPanel.setLayout(new javax.swing.BoxLayout(parmPanel, javax.swing.BoxLayout.PAGE_AXIS));

                parmPanel.setToolTipText(parm.getDescription());
                
                tabPanel.add(javax.swing.Box.createRigidArea(new Dimension(0,20)));
                tabPanel.add(parmPanel);
                tabPanel.add(javax.swing.Box.createRigidArea(new Dimension(0,20)));

                javax.swing.JPanel labelPanel = new javax.swing.JPanel();

                labelPanel.setLayout(new javax.swing.BoxLayout(labelPanel, javax.swing.BoxLayout.LINE_AXIS));

                labelPanel.add(javax.swing.Box.createRigidArea(preFill));
                javax.swing.JLabel minLabel = new javax.swing.JLabel();

                minLabel.setText(Double.toString(parm.getRange().getMin()));
                minLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEADING);
                labelPanel.add(minLabel);
                labelPanel.add(javax.swing.Box.createRigidArea(horizFill));

                labelPanel.add(javax.swing.Box.createHorizontalGlue());

                final javax.swing.JLabel nameLabel = new javax.swing.JLabel();

                nameLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

                nameLabel.setText(parm.getName() + " = " + numFormat.format(parm.map(50)));

                nameLabel.setFont(font);
                nameLabel.setToolTipText(parm.getDescription());
                labelPanel.add(nameLabel);
                labelPanel.add(javax.swing.Box.createHorizontalGlue());

                labelPanel.add(javax.swing.Box.createRigidArea(horizFill));
                javax.swing.JLabel maxLabel = new javax.swing.JLabel();

                maxLabel.setText(Double.toString(parm.getRange().getMax()));
                maxLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
                labelPanel.add(maxLabel);
                labelPanel.add(javax.swing.Box.createRigidArea(preFill));

                parmPanel.add(labelPanel);
                parmPanel.add(javax.swing.Box.createRigidArea(vertFill));
                final javax.swing.JSlider parmSlider = new javax.swing.JSlider();

                parmSlider.addChangeListener(new javax.swing.event.ChangeListener() {

                    public void stateChanged(javax.swing.event.ChangeEvent evt) {

                        int x = parmSlider.getValue();
                        parm.update(x);

                        nameLabel.setText(parm.getName() + " = " +  numFormat.format(parm.map(x)));

                    }
                });

                parmSlider.setToolTipText(parm.getDescription());

                parmPanel.add(parmSlider);

            }
            
            parmPane.addTab(group, tabPanel);

//            javax.swing.GroupLayout tabLayout = new javax.swing.GroupLayout(tabPanel);
//            tabPanel.setLayout(tabLayout);
//
//            tabLayout.setAutoCreateGaps(true);
//
//            // Turn on automatically creating gaps between components that touch
//            // the edge of the container and the container.
//            tabLayout.setAutoCreateContainerGaps(true);
//
//            javax.swing.GroupLayout.SequentialGroup horizGroup = tabLayout.createSequentialGroup();
//            javax.swing.GroupLayout.SequentialGroup vertGroup = tabLayout.createSequentialGroup();
//
//            javax.swing.GroupLayout.ParallelGroup nameGroup = tabLayout.createParallelGroup();
//            javax.swing.GroupLayout.ParallelGroup sliderGroup = tabLayout.createParallelGroup();
//
//
//            for (final SimParameter parm : parmGroups.get(group)) {
//                javax.swing.GroupLayout.ParallelGroup parmGroup = tabLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE);
//
//                final javax.swing.JSlider parmSlider = new javax.swing.JSlider();
//
//                parmSlider.addChangeListener(new javax.swing.event.ChangeListener() {
//
//                    public void stateChanged(javax.swing.event.ChangeEvent evt) {
//
//                        int x = parmSlider.getValue();
//                        parm.update(x);
//                    }
//                });
//
//                javax.swing.JLabel nameLabel = new javax.swing.JLabel();
//
//                nameLabel.setText(parm.getName());
//
//                parmGroup.addComponent(nameLabel);
//                nameGroup.addComponent(nameLabel);
//                sliderGroup.addComponent(parmSlider);
//
//                parmGroup.addComponent(parmSlider);
//
//                vertGroup.addGroup(parmGroup);
//            }
//
//            horizGroup.addGroup(nameGroup);
//            horizGroup.addGroup(sliderGroup);
//
//            tabLayout.setHorizontalGroup(horizGroup);
//            tabLayout.setVerticalGroup(vertGroup);


        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Parameters");
        setFont(new java.awt.Font("Arial", 0, 36)); // NOI18N

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

        final List<SimParameter> testParms = new ArrayList<SimParameter>();

        // create some test parms
            SimParameter test1 = new SimParameter("savings rate", "by golthars hammer - oh what a savings", "Savings",
                new SimParameter.Method() {
            public void update(double val) {
                test(val);
            }
        });

        test1.setRange(0, 1);
        testParms.add(test1);

        // create some test parms
            SimParameter test2 = new SimParameter("other savings parm", "by golthars hammer - oh what a savings", "Savings",
                new SimParameter.Method() {
            public void update(double val) {
                test(val);
            }
        });

        test2.setRange(-10, 100);
        testParms.add(test2);


        // create some test parms
            SimParameter test3 = new SimParameter("yet another savings", "by golthars hammer - oh what a savings", "Savings",
                new SimParameter.Method() {
            public void update(double val) {
                test(val);
            }
        });

        test3.setRange(0, 1000);
        testParms.add(test3);


          // create some test parms
            SimParameter test4 = new SimParameter("intereset rate", "by golthars hammer - oh what a savings", "Interest",
                new SimParameter.Method() {
            public void update(double val) {
                test(val);
            }
        });

        test4.setRange(-1000, 1000);
        testParms.add(test4);


          // create some test parms
            SimParameter test5 = new SimParameter("some other long name", "by golthars hammer - oh what a savings", "Interest",
                new SimParameter.Method() {
            public void update(double val) {
                test(val);
            }
        });

        testParms.add(test5);

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ParmView(testParms).setVisible(true);
            }
        });
    }

    // test methods
    private static void test(double val) {
        System.out.print(val);
        System.out.print("\n");
    }

      private javax.swing.JTabbedPane parmPane;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
